<template>
  <el-tabs type="border-card"
           @tab-click="fresh">
    <el-tab-pane label="用户举报">
      <div class="app-container">
        <el-form v-show="showSearch"
                 ref="RqueryForm"
                 :model="queryParamsG"
                 :inline="true"
                 label-width="68px">
          <el-form-item label="帖子id"
                        prop="articleid">
            <el-input v-model="queryParamsG.articleid"
                      placeholder="请输入文章id"
                      clearable
                      size="small"
                      @keyup.enter.native="RhandleQuery" />
          </el-form-item>
          <el-form-item label="举报人id"
                        prop="reporterid">
            <el-input v-model="queryParamsG.reporterid"
                      placeholder="请输入举报人id"
                      clearable
                      size="small"
                      @keyup.enter.native="RhandleQuery" />
          </el-form-item>
          <el-form-item label="举报类型"
                        prop="type">
            <el-select v-model="queryParamsG.type"
                       placeholder="请选择举报类型"
                       clearable
                       size="small">
              <el-option label="请选择字典生成"
                         value="" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注"
                        prop="memo">
            <el-input v-model="queryParamsG.memo"
                      placeholder="请输入备注"
                      clearable
                      size="small"
                      @keyup.enter.native="RhandleQuery" />
          </el-form-item>
          <el-form-item label="是否处理"
                        prop="isprocess">
            <el-input v-model="queryParamsG.isprocess"
                      placeholder="请输入是否处理"
                      clearable
                      size="small"
                      @keyup.enter.native="RhandleQuery" />
          </el-form-item>
          <el-form-item label="社区id"
                        prop="communityid">
            <el-input v-model="queryParamsG.communityid"
                      placeholder="请输入社区id"
                      clearable
                      size="small"
                      @keyup.enter.native="RhandleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary"
                       icon="el-icon-search"
                       size="mini"
                       @click="RhandleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh"
                       size="mini"
                       @click="RresetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10"
                class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:report:add']"
                       type="primary"
                       plain
                       icon="el-icon-plus"
                       size="mini"
                       @click="RhandleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:report:edit']"
                       type="success"
                       plain
                       icon="el-icon-edit"
                       size="mini"
                       :disabled="single"
                       @click="RhandleUpdate">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:report:remove']"
                       type="danger"
                       plain
                       icon="el-icon-delete"
                       size="mini"
                       :disabled="Rmultiple"
                       @click="RhandleDelete">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:report:export']"
                       type="warning"
                       plain
                       icon="el-icon-download"
                       size="mini"
                       @click="RhandleExport">导出</el-button>
          </el-col>
          <right-toolbar :show-search.sync="RshowSearch"
                         @queryTable="RgetList" />
        </el-row>

        <el-table v-loading="Rloading"
                  :data="reportList"
                  @selection-change="RhandleSelectionChange">
          <el-table-column type="selection"
                           width="55"
                           align="center" />
          <el-table-column label="id"
                           align="center"
                           prop="id" />
          <el-table-column label="文章id"
                           align="center"
                           prop="articleid" />
          <el-table-column label="举报人id"
                           align="center"
                           prop="reporterid" />
          <el-table-column label="举报类型"
                           align="center"
                           prop="type" />
          <el-table-column label="备注"
                           align="center"
                           prop="memo" />
          <el-table-column label="是否处理"
                           align="center"
                           prop="isprocess" />
          <el-table-column label="社区id"
                           align="center"
                           prop="communityid" />
          <el-table-column label="操作"
                           align="center"
                           class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button v-hasPermi="['system:report:edit']"
                         size="mini"
                         type="text"
                         icon="el-icon-edit"
                         @click="RhandleUpdate(scope.row)">修改</el-button>
              <el-button size="mini"
                         type="text"
                         icon="el-icon-document"
                         @click="toArticle(scope.row.articleid)">详情</el-button>
              <el-button v-hasPermi="['system:report:remove']"
                         size="mini"
                         type="text"
                         icon="el-icon-delete"
                         @click="RhandleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="Rtotal>0"
                    :total="Rtotal"
                    :page.sync="queryParamsG.pageNum"
                    :limit.sync="queryParamsG.pageSize"
                    @pagination="RgetList" />

        <!-- 添加或修改论坛举报对话框 -->
        <el-dialog :title="Rtitle"
                   :visible.sync="Ropen"
                   width="500px"
                   append-to-body>
          <el-form ref="form"
                   :model="Rform"
                   :rules="Rrules"
                   label-width="80px">
            <el-form-item label="文章id"
                          prop="articleid">
              <el-input v-model="Rform.articleid"
                        placeholder="请输入文章id" />
            </el-form-item>
            <el-form-item label="举报人id"
                          prop="reporterid">
              <el-input v-model="Rform.reporterid"
                        placeholder="请输入举报人id" />
            </el-form-item>
            <el-form-item label="举报类型"
                          prop="type">
              <el-select v-model="Rform.type"
                         placeholder="请选择举报类型">
                <el-option label="请选择字典生成"
                           value="" />
              </el-select>
            </el-form-item>
            <el-form-item label="备注"
                          prop="memo">
              <el-input v-model="Rform.memo"
                        placeholder="请输入备注" />
            </el-form-item>
            <el-form-item label="是否处理"
                          prop="isprocess">
              <el-input v-model="Rform.isprocess"
                        placeholder="请输入是否处理" />
            </el-form-item>
            <el-form-item label="社区id"
                          prop="communityid">
              <el-input v-model="Rform.communityid"
                        placeholder="请输入社区id" />
            </el-form-item>
          </el-form>
          <div slot="footer"
               class="dialog-footer">
            <el-button type="primary"
                       @click="RsubmitForm">确 定</el-button>
            <el-button @click="Rcancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-tab-pane>
    <el-tab-pane label="惩罚详情">
      <div class="app-container">
        <el-form v-show="showSearch"
                 ref="queryForm"
                 :model="queryParams"
                 :inline="true"
                 label-width="68px">
          <el-form-item label="帖子id"
                        prop="articleid">
            <el-input v-model="queryParams.articleid"
                      placeholder="请输入帖子id"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="操作者id"
                        prop="operatorid">
            <el-input v-model="queryParams.operatorid"
                      placeholder="请输入操作者id"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="被罚者id"
                        prop="punishedid">
            <el-input v-model="queryParams.punishedid"
                      placeholder="请输入被罚者id"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="备注"
                        prop="memo">
            <el-input v-model="queryParams.memo"
                      placeholder="请输入备注"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="是否删帖"
                        prop="isdelete">
            <el-input v-model="queryParams.isdelete"
                      placeholder="请输入是否删帖"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="是否禁言"
                        prop="ismute">
            <el-input v-model="queryParams.ismute"
                      placeholder="请输入是否禁言"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="操作时间"
                        prop="operatetime">
            <el-date-picker v-model="queryParams.operatetime"
                            clearable
                            size="small"
                            type="date"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择操作时间" />
          </el-form-item>
          <el-form-item label="禁言截至时间"
                        prop="speaktime">
            <el-date-picker v-model="queryParams.speaktime"
                            clearable
                            size="small"
                            type="date"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择禁言截至时间" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary"
                       icon="el-icon-search"
                       size="mini"
                       @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh"
                       size="mini"
                       @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10"
                class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:punish:add']"
                       type="primary"
                       plain
                       icon="el-icon-plus"
                       size="mini"
                       @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:punish:edit']"
                       type="success"
                       plain
                       icon="el-icon-edit"
                       size="mini"
                       :disabled="single"
                       @click="handleUpdate">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:punish:remove']"
                       type="danger"
                       plain
                       icon="el-icon-delete"
                       size="mini"
                       :disabled="multiple"
                       @click="handleDelete">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['system:punish:export']"
                       type="warning"
                       plain
                       icon="el-icon-download"
                       size="mini"
                       @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar :show-search.sync="showSearch"
                         @queryTable="getList" />
        </el-row>

        <el-table v-loading="loading"
                  :data="punishList"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection"
                           width="55"
                           align="center" />
          <el-table-column label="id"
                           align="center"
                           prop="id" />
          <el-table-column label="帖子id"
                           align="center"
                           prop="articleid" />
          <el-table-column label="操作者id"
                           align="center"
                           prop="operatorid" />
          <el-table-column label="操作者名称"
                           align="center"
                           prop="operatorname" />
          <el-table-column label="被罚者id"
                           align="center"
                           prop="punishedid" />
          <el-table-column label="被罚者名称"
                           align="center"
                           prop="punishedname" />
          <el-table-column label="备注"
                           align="center"
                           prop="memo" />
          <el-table-column label="是否删帖"
                           align="center"
                           prop="isdelete" />
          <el-table-column label="是否禁言"
                           align="center"
                           prop="ismute" />
          <el-table-column label="操作时间"
                           align="center"
                           prop="operatetime"
                           width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.operatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="禁言截至时间"
                           align="center"
                           prop="speaktime"
                           width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.speaktime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作"
                           align="center"
                           class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button v-hasPermi="['system:punish:edit']"
                         size="mini"
                         type="text"
                         icon="el-icon-edit"
                         @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini"
                         type="text"
                         icon="el-icon-document"
                         @click="toArticle(scope.row.articleid)">详情</el-button>
              <el-button v-hasPermi="['system:punish:remove']"
                         size="mini"
                         type="text"
                         icon="el-icon-delete"
                         @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList" />

        <!-- 添加或修改社区惩罚对话框 -->
        <el-dialog :title="title"
                   :visible.sync="open"
                   width="500px"
                   append-to-body>
          <el-form ref="form"
                   :model="form"
                   :rules="rules"
                   label-width="80px">
            <el-form-item label="帖子id"
                          prop="articleid">
              <el-input v-model="form.articleid"
                        placeholder="请输入帖子id" />
            </el-form-item>
            <el-form-item label="操作者id"
                          prop="operatorid">
              <el-input v-model="form.operatorid"
                        placeholder="请输入操作者id" />
            </el-form-item>
            <el-form-item label="被罚者id"
                          prop="punishedid">
              <el-input v-model="form.punishedid"
                        placeholder="请输入被罚者id" />
            </el-form-item>
            <el-form-item label="备注"
                          prop="memo">
              <el-input v-model="form.memo"
                        placeholder="请输入备注" />
            </el-form-item>
            <el-form-item label="是否删帖"
                          prop="isdelete">
              <el-input v-model="form.isdelete"
                        placeholder="请输入是否删帖" />
            </el-form-item>
            <el-form-item label="是否禁言"
                          prop="ismute">
              <el-input v-model="form.ismute"
                        placeholder="请输入是否禁言" />
            </el-form-item>
            <el-form-item label="操作时间"
                          prop="operatetime">
              <el-date-picker v-model="form.operatetime"
                              clearable
                              size="small"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择操作时间" />
            </el-form-item>
            <el-form-item label="禁言截至时间"
                          prop="speaktime">
              <el-date-picker v-model="form.speaktime"
                              clearable
                              size="small"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择禁言截至时间" />
            </el-form-item>
          </el-form>
          <div slot="footer"
               class="dialog-footer">
            <el-button type="primary"
                       @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import { listPunish, getPunish, delPunish, addPunish, updatePunish, exportPunish } from '@/api/bbs/punish'
import { listReport, getReport, delReport, addReport, updateReport, exportReport } from '@/api/bbs/report'

export default {
  name: 'Punish',
  components: {
  },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社区惩罚表格数据
      punishList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleid: null,
        operatorid: null,
        punishedid: null,
        memo: null,
        isdelete: null,
        ismute: null,
        operatetime: null,
        speaktime: null,
        communityid: null,
        fromsource: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleid: [
          { required: true, message: '帖子id不能为空', trigger: 'blur' }
        ],
        operatorid: [
          { required: true, message: '操作者id不能为空', trigger: 'blur' }
        ],
        punishedid: [
          { required: true, message: '被罚者id不能为空', trigger: 'blur' }
        ],
        isdelete: [
          { required: true, message: '是否删帖不能为空', trigger: 'blur' }
        ],
        ismute: [
          { required: true, message: '是否禁言不能为空', trigger: 'blur' }
        ],
        operatetime: [
          { required: true, message: '操作时间不能为空', trigger: 'blur' }
        ],
        speaktime: [
          { required: true, message: '禁言截至时间不能为空', trigger: 'blur' }
        ]
      },
      // //
      // 遮罩层
      Rloading: true,
      // 选中数组
      Rids: [],
      // 非单个禁用
      Rsingle: true,
      // 非多个禁用
      Rmultiple: true,
      // 显示搜索条件
      RshowSearch: true,
      // 总条数
      Rtotal: 0,
      // 论坛举报表格数据
      reportList: [],
      // 弹出层标题
      Rtitle: '',
      // 是否显示弹出层
      Ropen: false,
      // 查询参数
      queryParamsG: {
        pageNum: 1,
        pageSize: 10,
        articleid: null,
        reporterid: null,
        type: null,
        memo: null,
        isprocess: null,
        communityid: null,
        fromsource: 1
      },
      // 表单参数
      Rform: {},
      // 表单校验
      Rrules: {
        articleid: [
          { required: true, message: '文章id不能为空', trigger: 'blur' }
        ],
        reporterid: [
          { required: true, message: '举报人id不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '举报类型不能为空', trigger: 'change' }
        ],
        isprocess: [
          { required: true, message: '是否处理不能为空', trigger: 'blur' }
        ],
        communityid: [
          { required: true, message: '社区id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.RgetList()
    this.getList()
    this.RgetList()
  },
  methods: {
    /** 查询社区惩罚列表 */
    getList () {
      this.loading = true
      listPunish(this.queryParams).then(response => {
        this.punishList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        articleid: null,
        operatorid: null,
        punishedid: null,
        memo: null,
        isdelete: null,
        ismute: null,
        operatetime: null,
        speaktime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加社区惩罚'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const id = row.id || this.ids
      console.log(id)
      getPunish(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改社区惩罚'
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePunish(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPunish(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除社区惩罚编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delPunish(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有社区惩罚数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportPunish(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    toArticle (id) {
      this.$router.push('/bbs/detail/' + id)
    },
    fresh (tab, event) {
      if (tab.index == 1) {
        this.getList()
      }
      if (tab.index == 0) {
        this.RgetList()
      }
    },
    RgetList () {
      this.Rloading = true
      listReport(this.queryParamsG).then(response => {
        this.reportList = response.rows
        this.Rtotal = response.total
        this.Rloading = false
      })
    },
    // 取消按钮
    Rcancel () {
      this.Ropen = false
      this.Rreset()
    },
    // 表单重置
    Rreset () {
      this.Rform = {
        id: null,
        articleid: null,
        reporterid: null,
        type: null,
        memo: null,
        isprocess: null,
        communityid: null
      }
      this.RresetForm('form')
    },
    /** 搜索按钮操作 */
    RhandleQuery () {
      this.queryParamsG.pageNum = 1
      this.RgetList()
    },
    /** 重置按钮操作 */
    RresetQuery () {
      this.RresetForm('RqueryForm')
      this.RhandleQuery()
    },
    // 多选框选中数据
    RhandleSelectionChange (selection) {
      this.Rids = selection.map(item => item.id)
      this.Rsingle = selection.length !== 1
      this.Rmultiple = !selection.length
    },
    /** 新增按钮操作 */
    RhandleAdd () {
      this.Rreset()
      this.Ropen = true
      this.Rtitle = '添加论坛举报'
    },
    /** 修改按钮操作 */
    RhandleUpdate (row) {
      this.Rreset()
      const id = row.id || this.Rids
      getReport(id).then(response => {
        this.Rform = response.data
        this.Ropen = true
        this.Rtitle = '修改论坛举报'
      })
    },
    /** 提交按钮 */
    RsubmitForm () {
      this.$refs['Rform'].validate(valid => {
        if (valid) {
          if (this.Rform.id != null) {
            updateReport(this.Rform).then(response => {
              this.msgSuccess('修改成功')
              this.Ropen = false
              this.RgetList()
            })
          } else {
            addReport(this.Rform).then(response => {
              this.msgSuccess('新增成功')
              this.Ropen = false
              this.RgetList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    RhandleDelete (row) {
      const ids = row.id || this.Rids
      this.$confirm('是否确认删除论坛举报编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delReport(ids)
      }).then(() => {
        this.RgetList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    RhandleExport () {
      const queryParams = this.queryParamsG
      this.$confirm('是否确认导出所有论坛举报数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportReport(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }

  }
}
</script>
